package mvp.com.bbc.rcjavaretrofitdaggermvp.mvp.fragment;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.app.Fragment;
import android.support.v4.util.Pair;
import android.support.v4.view.ViewPager;
import android.transition.Explode;
import android.view.View;
import android.widget.RelativeLayout;
import com.qslll.library.ExpandingPagerFactory;
import com.qslll.library.fragments.ExpandingFragment;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.Unbinder;
import mvp.com.bbc.rcjavaretrofitdaggermvp.R;
import mvp.com.bbc.rcjavaretrofitdaggermvp.baseAF.BaseFragment;
import mvp.com.bbc.rcjavaretrofitdaggermvp.bean.Travel;
import mvp.com.bbc.rcjavaretrofitdaggermvp.mvp.activity.InfoActivity;
import mvp.com.bbc.rcjavaretrofitdaggermvp.mvp.adapter.TravelViewPagerAdapter;

/**
 * Created by ${cbf} on 2017/11/24.
 */

public class MyFragment extends BaseFragment implements ExpandingFragment.OnExpandingClickListener {


    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.back)
    RelativeLayout back;

    @Override
    protected int setContentLayout() {
        return R.layout.my_fragment;
    }

    @Override
    protected void findViews() {

    }

    @Override
    protected void init() {
        //setupWindowAnimations();
        TravelViewPagerAdapter adapter = new TravelViewPagerAdapter(getChildFragmentManager());
        adapter.addAll(generateTravelList());
        viewPager.setAdapter(adapter);

        ExpandingPagerFactory.setupViewPager(viewPager);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                ExpandingFragment expandingFragment = ExpandingPagerFactory.getCurrentFragment(viewPager);
                if (expandingFragment != null && expandingFragment.isOpenend()) {
                    expandingFragment.close();
                }
            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void setupWindowAnimations() {
        Explode slideTransition = new Explode();
        getActivity().getWindow().setReenterTransition(slideTransition);
        getActivity().getWindow().setExitTransition(slideTransition);
    }

    private List<Travel> generateTravelList() {
        List<Travel> travels = new ArrayList<>();
        for (int i = 0; i < 5; ++i) {
            travels.add(new Travel("Seychelles", R.drawable.seychelles));
            travels.add(new Travel("Shang Hai", R.drawable.shh));
            travels.add(new Travel("New York", R.drawable.newyork));
            travels.add(new Travel("castle", R.drawable.p1));
        }
        return travels;
    }

    @SuppressWarnings("unchecked")
    private void startInfoActivity(View view, Travel travel) {
        ActivityCompat.startActivity(getActivity(),
                InfoActivity.newInstance(getActivity(), travel),
                ActivityOptionsCompat.makeSceneTransitionAnimation(
                        getActivity(),
                        new Pair<>(view, getString(R.string.transition_image)))
                        .toBundle());
    }

    @Override
    public void onExpandingClick(View v) {
        View view = v.findViewById(R.id.image);
        Travel travel = generateTravelList().get(viewPager.getCurrentItem());
        startInfoActivity(view, travel);
    }

    @Override
    protected void setListeners() {

    }

    @Override
    public void onClick(View view) {

    }

    public static Fragment Instance(Context context, String my) {
        MyFragment homeFragment = new MyFragment();
        Bundle bundle = new Bundle();
        bundle.putString("name", my);
        homeFragment.setArguments(bundle);
        return homeFragment;
    }
}
